//
//
//

#define GAME_DEBUG

#include <stdlib.h>
#include <string>
#include <iostream>
#include "CGameClient.h"
#include "CCombatHandler.h"
#include "Lua Loader/CLuaLoader.h"

using namespace irr;

using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;

using	std::string;

CGameClient* CGameClient::m_pGameInstance = 0;

CGameClient::CGameClient()
{
	m_pIrrlicht = NULL;
}

CGameClient*	CGameClient::Instance()
{
	if ( m_pGameInstance == NULL )
	{
		m_pGameInstance = new CGameClient;
	}
	return	m_pGameInstance;
}

bool	CGameClient::Init( std::string url )
{
	m_pIrrlicht = createDevice( video::EDT_DIRECT3D9, dimension2d<u32>(1024,768), 32, false, false, false, 0 );
	m_pIrrlicht->setWindowCaption(L"Action Game - Swdgame Studio");

	//Initializing Bullet Physics
	m_pBulletPhysics = new CBulletPhysics;
	m_pBulletPhysics->Init();



	//set event handler
	m_pIrrlicht->setEventReceiver( CCombatHandler::Instance() );

	int seat_id;
	int avatar_num;
	int table_id;
	std::string avatar_name;

	std::string tmp;
	int star_count = 0;
// 
// 	tmp.clear();
// 	for ( int i = 0; i < url.size(); i++ )
// 	{
// 		if ( ( url[i] != '*' ) && ( i > 6 ) )
// 		{
// 			tmp.push_back( url[i] );
// 		}
// 		else
// 		{
// 			star_count = star_count++;
// 			if ( star_count == 1 )
// 			{
// 				table_id = atoi( tmp.c_str() );
// 				tmp.clear();
// 			}
// 			else	if ( star_count == 2 )
// 			{
// 				seat_id = atoi( tmp.c_str() );
// 				tmp.clear();
// 			}
// 			else	if ( star_count == 3 )
// 			{
// 				avatar_name = tmp;
// 				tmp.clear();
// 			}
// 			else	if ( star_count == 4 )
// 			{
// 				avatar_num = atoi( tmp.c_str() );
// 				tmp.clear();
// 			}
// 		}
// 	}

#ifdef GAME_DEBUG
	ISceneManager*	sm = m_pIrrlicht->getSceneManager();
	IVideoDriver*	dr = m_pIrrlicht->getVideoDriver();
	
	std::cout << "OK\n";

	std::string	s;
	
	CLuaLoader*	lua = CLuaLoader::Instance();
	lua->LoadFile( -1, "D:/Swdgame Studio/junior/oo/code/svn/oo-project-2010-2011/action/resource/debug_config.lua" );

	dr->setTextureCreationFlag( ETCF_ALWAYS_32_BIT, true );

	s = lua->GetString( 0, "test_mesh" );
	//IAnimatedMesh*	mesh = sm->getMesh( "../resource/knight.dae" );
	IAnimatedMesh* mesh = sm->getMesh( s.c_str() );

	//int avatar_num = 1;


	IAnimatedMeshSceneNode* x = sm->addAnimatedMeshSceneNode( mesh );
	x->setMaterialFlag( EMF_LIGHTING, false );
	//x->getMaterial(0).setTexture( 0, dr->getTexture( "../resource/sydney.bmp" ) );
	x->getMaterial(0).Lighting = false;
	x->setPosition( vector3df( 50, 0, 50 ) );
	x->setScale( vector3df(1, 1, 1) );
	x->setAnimationSpeed(0);

	//CAvatar* avatar_1 = x;
	this->m_vAvatars.push_back( x );

	IAnimatedMeshSceneNode*	y = sm->addAnimatedMeshSceneNode( mesh );
	y->setMaterialFlag( EMF_LIGHTING, false );
	y->getMaterial(0).Lighting = false;
	y->setPosition( vector3df( 100, 0, 100 ) );
	y->setScale( vector3df(1,1,1) );
	y->setRotation( vector3df( 0, 180, 0) );
	y->setAnimationSpeed(0);
	m_vAvatars.push_back(y);

	IAnimatedMeshSceneNode*	z = sm->addAnimatedMeshSceneNode( mesh );
	z->setMaterialFlag( EMF_LIGHTING, false );
	z->getMaterial(0).Lighting = false;
	z->setPosition( vector3df( 50, 0, 100 ) );
	z->setScale( vector3df(1,1,1) );
	z->setRotation( vector3df( 0, 90, 0) );
	z->setAnimationSpeed(0);
	m_vAvatars.push_back(z);

	IAnimatedMeshSceneNode* v = sm->addAnimatedMeshSceneNode( mesh );
	v->setMaterialFlag( EMF_LIGHTING, false );
	v->getMaterial(0).Lighting = false;
	v->setPosition( vector3df( 100, 0, 50 ) );
	v->setScale( vector3df(1,1,1) );
	v->setRotation( vector3df( 0, 270, 0) );
	v->setAnimationSpeed(0);
	//CAvatar*	av = v;
	m_vAvatars.push_back(v);

	
	ITerrainSceneNode* terrain = sm->addTerrainSceneNode( "D:/Swdgame Studio/junior/oo/code/svn/oo-project-2010-2011/action/resource/terrain-heightmap.bmp", 0, -1,
		vector3df(-500,0,-500),	vector3df(0,0,0), vector3df(1000,0,1000));
	//ITexture*	
	terrain->setMaterialFlag( EMF_LIGHTING, false );
	terrain->getMaterial(0).setTexture( 0, dr->getTexture( "D:/Swdgame Studio/junior/oo/code/svn/oo-project-2010-2011/action/resource/terrain-texture.jpg" ) );
	//terrain->getMaterial(0).Lighting = false;
	terrain->setVisible(true);

	
	s = lua->GetString(0, "skydome_texture");
	sm->addSkyDomeSceneNode( dr->getTexture( s.c_str() ) );

	ICameraSceneNode* camera = sm->addCameraSceneNodeFPS();
	camera->setPosition( vector3df(0,10,0) );


	m_pNetwork = new CClientLinker;
	m_pNetwork->Init();

	m_pGameState = CCombatHandler::Instance();
#endif


	return true;
}

bool	CGameClient::Run()
{
	IVideoDriver*			driver = m_pIrrlicht->getVideoDriver();
	IGUIEnvironment*		gui = m_pIrrlicht->getGUIEnvironment();
	ISceneManager*		scene_manager = m_pIrrlicht->getSceneManager();
	while (m_pIrrlicht->run())
	{
		this->m_pGameState->Update();

		driver->beginScene(true, true, SColor(255,125,125,125));

		scene_manager->drawAll();
		gui->drawAll();

		driver->endScene();

		//m_pGameInstance->Update();
	}

	return	true;
}
